﻿using System;
using System.Collections.Generic;
using System.Drawing;
using RaytracerLib.Layers;
using RaytracerLib.MathLib;
using TomanuExtensions;
using RaytracerLib.Layers.OverlayCorrectors;
using System.Linq;
using RaytracerLib.Postprocessing;
using YAXLib;
using RaytracerLib.Resamplers.Filters;

namespace RaytracerLib.Resamplers
{
    public abstract class FilterResampler : Resampler
    {
        [YAXNode]
        public Filter Filter = new BoxFilter();

        internal override double FilterRay
        {
            get
            {
                return Filter.Ray;
            }
        }

        internal override void RenderStart(RenderStartPhase a_phase)
        {
            if (a_phase == RenderStartPhase.PrepareObjectToRender)
            {
                if (Filter.FilterType == FilterType.NearestNeighbour)
                    throw new InvalidOperationException();
            }
        }

        internal override void Resample(Rectangle a_rect, ColorArrayFloat a_dest)
        {
            for (int y = a_rect.Top; y < a_rect.Bottom; y++)
            {
                for (int x = a_rect.Left; x < a_rect.Right; x++)
                {
                    a_dest.SetColor(x, y, ResamplePixel(x, y));
                }
            }
        }

        /// <summary>
        /// Note: Return color in linear space.
        /// </summary>
        /// <param name="a_x"></param>
        /// <param name="a_y"></param>
        /// <returns></returns>
        protected abstract ColorFloat ResamplePixel(int a_x, int a_y);
    }
}